#!/usr/bin/env bash

#
# dtb_list extracts the list of DTB files from BR2_LINUX_KERNEL_INTREE_DTS_NAME
# in ${BR_CONFIG}, then prints the corresponding list of file names for the
# genimage configuration file
#
dtb_list()
{
	local DTB_LIST

	DTB_LIST="$(sed -n 's/^BR2_LINUX_KERNEL_INTREE_DTS_NAME="\([\/a-z0-9 \-]*\)"$/\1/p' "${BR2_CONFIG}")"

	for dt in $DTB_LIST; do
		echo -n "\"$(basename "${dt}").dtb\", "
	done
}

#
# linux_image extracts the Linux image format from BR2_LINUX_KERNEL_UIMAGE in
# ${BR_CONFIG}, then prints the corresponding file name for the genimage
# configuration file
#
linux_image()
{
	if grep -Eq "^BR2_LINUX_KERNEL_IMAGE=y$" "${BR2_CONFIG}"; then
		echo "\"Image\""
	else
		echo "\"zImage\""
	fi
}

genimage_type()
{
	if grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G2=y$" "${BR2_CONFIG}"; then
		echo "genimage.cfg.template_s32g2"
	elif grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G3=y$" "${BR2_CONFIG}"; then
		echo "genimage.cfg.template_s32g3"
	else
		echo "genimage.cfg.template"
	fi
}

fip_image()
{
	if grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G2=y$" "${BR2_CONFIG}"; then
		echo "fip.s32"
	elif grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G3=y$" "${BR2_CONFIG}"; then
		echo "fip.s32"
	elif grep -Eq "^BR2_TARGET_UBOOT_FORMAT_IMG=y$" "${BR2_CONFIG}"; then
	    echo "u-boot.img"
    else
        echo "fip.s32"
	fi
}

ivt_header_image()
{
	if grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G2=y$" "${BR2_CONFIG}"; then
		echo "ivt_header.bin"
	elif grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G3=y$" "${BR2_CONFIG}"; then
		echo "ivt_header.bin"
    else
        echo "ivt_header.bin"
	fi
}

boot_image()
{
	if grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G2=y$" "${BR2_CONFIG}"; then
		echo "bootloader.bin"
	elif grep -Eq "^BR2_PACKAGE_NXP_S32_PLATFORM_S32G3=y$" "${BR2_CONFIG}"; then
		echo "bootloader.bin"
    else
        echo "bootloader.bin"
	fi
}

get_image_size()
{
    local IMAGE="${BINARIES_DIR}/$1"

    if [[ -f "$IMAGE" ]]; then
        echo $(stat -c %s "$IMAGE")  # 返回文件大小（以字节为单位）
    else
        echo "0"  # 如果文件不存在，返回0
    fi
}

get_ivt_header_image()
{
    local INPUT_IMAGE="${BINARIES_DIR}/$1"
    local OUTPUT_IMAGE="${BINARIES_DIR}/$2"

    dd if="$INPUT_IMAGE" of="$OUTPUT_IMAGE" bs=256 count=1 seek=0
}

get_boot_image()
{
    local INPUT_IMAGE="${BINARIES_DIR}/$1"
    local OUTPUT_IMAGE="${BINARIES_DIR}/$2"

    dd if="$INPUT_IMAGE" of="$OUTPUT_IMAGE" bs=512 seek=0 skip=1
}

main()
{
	local GENIMAGE_CFG GENIMAGE_TMP FIP_S32_SIZE
    local FIP_S32 FIP_S32_SIZE
    local FILES
    local IVT_HEADER BOOTLOADER

	FILES="$(dtb_list) $(linux_image)"

    # 获取 IVT 头和 boot 实体
    FIP_S32="$(fip_image)"
    IVT_HEADER="$(ivt_header_image)"
    BOOTLOADER="$(boot_image)"

    FIP_S32_SIZE="$(get_image_size "$FIP_S32")"
    echo "FIP_S32_SIZE is $FIP_S32_SIZE"
    $(get_ivt_header_image "$FIP_S32" "$IVT_HEADER")
    $(get_boot_image "$FIP_S32" "$BOOTLOADER")

	GENIMAGE_CFG="$(mktemp --suffix genimage.cfg)"
	GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp"

	sed -e "s/%FILES%/${FILES}/" \
		-e "s/%IVT_HEADER%/${IVT_HEADER}/" \
        -e "s/%BOOTLOADER%/${BOOTLOADER}/" \
		"board/alpha/common/s32/$(genimage_type)" > "${GENIMAGE_CFG}"

	rm -rf "${GENIMAGE_TMP}"

	genimage \
		--rootpath "${TARGET_DIR}" \
		--tmppath "${GENIMAGE_TMP}" \
		--inputpath "${BINARIES_DIR}" \
		--outputpath "${BINARIES_DIR}" \
		--config "${GENIMAGE_CFG}"

	rm -f "${GENIMAGE_CFG}"
    rm -rf "${BINARIES_DIR}/${IVT_HEADER}"
    rm -rf "${BINARIES_DIR}/${BOOTLOADER}"

	exit $?
}

main "$@"
